<?php
namespace MyAdmin\Controller;

use Think\Controller;

class UsersController extends CommonController
{
  public function __construct()
  {
    parent::__construct();
    $this->check_login();
  }

  public function index()
  {
    $page = I("page", 1);
    $page_size = I("page_size", 10);
    $model=M("users");
    //查询
    $nickname = I("nickname");
    $types = I("types", '');

    //上架
    $where = "app_id={$this->app_id} ";
    if ($nickname) {//用户名
      $where .= " and nickname like '%".trim($nickname)."%'";
      $where_ar['nickname']=$nickname;
      $this->assign("nickname", $nickname);
    }
    if ($types!='') {//类型
      $where .= " and types=".trim($types);
      $where_ar['types']=$types;
      $this->assign("types", $types);
    }

    $field = "*";
    $order = "id desc";
    $list = $model->field($field)->where($where)->order($order)->page($page.",".$page_size)->select();
    foreach ($list as $key => &$value) {
      $value['created_at'] = local_time($value['created_at']);
      $value['updated_at'] = local_time($value['updated_at']);

      switch ($value['status']) {
        case 1:
          $value['status_str'] = "开启";
          break;
        case 2:
          $value['status_str'] = "关闭";
          break;
        default:
          # code...
          break;
      }
      switch ($value['types']) {
        case 1:
          $value['type_str'] = "商户";
          break;
        case 2:
          $value['type_str'] = "管理员";
          break;
        default:
          $value['type_str'] = "用户";
          break;
      }
    }

    $num = $model->where($where)->count();

    $page_list = get_html_page_list($url_name, $list, $page, $page_size, $num, $where_ar);
    $this->assign("page_list", $page_list);

    $this->display();
  }

  //改变状态
  public function change_status()
  {
    $model = M("users");
    $status = I("status", 2);
    $id = I("id", '');

    $post_data["status"] = $status;
    $post_data["updated_at"] = $this->now_time;
    $where = " id=".$id;
    
    $result = $model->where($where)->save($post_data);
    if ($result) {
      return_data("success", "change_status.users.success");
    } else {
      return_data("error", "change_status.users.error");
    }
  }
}
